// pages/institufirst/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
const http = require('../../utils/util.js')
var app = getApp()
var that
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showView: false,
		heart: true,
		alldetail: {
			status: 3
		}, //教师的详情
		allClass: [], //所有课程
		imgUrls: [],
		current: 0,
		time: '12:00',
		date: '2018-10-01',
		teacherlist: [],
		teacherlist1: [],
		actives: [{
				word1: '拼团'
			},
			{
				word1: '砍价'
			},
			{
				word1: '拼团',
			},
			{
				word1: '砍价',
			},
		],
		tabIcon: [{
				id: 0,
				pic: '/img/homes.png',
				piced: '/img/homesd.png',
				title: '主页',
			},
			{
				id: 1,
				pic: '/img/course.png',
				piced: '/img/coursed.png',
				title: '课程',
			},
			{
				id: 2,
				pic: '/img/mine.png',
				piced: '/img/mined.png',
				title: '我的',

			},
		],
		learnpeoNow: {
			name: '',
			nick: ''
		},
		teacherId: '',
		visitorId: '',
		showauth: true, //显示授权 
		userInfo: '',
		statusNum: '',
		myInfo: '', //个人信息
		studentList: [], //学生列表
		grate: [],
		learnpeoIndex: 0
	},

	seCurrent: function(e) {
		this.setData({
			current: e.currentTarget.dataset.id,
		})
		wx.pageScrollTo({
          scrollTop: 0,
          duration: 0
        })

	},
	//免费体验
	onChangeShowState: function(e) {
		var that = this;
		let index = e.currentTarget.dataset.index
		let teacherlist1 = this.data.teacherlist[index]
		that.setData({
			showView: !that.data.showView,
			teacherlist1: teacherlist1
		})
	},
	// 页面跳转课程详情
	classDetail: function(e) {
		let id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: '/pages/classdetail/index?courseId=' + id+'&from=3&teacherId='+this.data.teacherId,
		})

	},
	//我也在此學過
	haveLearned: function() {
		wx.navigateTo({
			url: '/pages/scoreteacher/index?teacherId=' + this.data.alldetail.teacherId,
		})

	},
	//  老师首页信息
	teacherTotal: function() {
		let that = this;
		app.request({
			url: 'teacher/total',
			data: {
				teacher_id: that.data.teacherId
			},
			sCallback: (res) => {
				that.setData({
					listcount: res.data
				})
			}
		})
	},
	//  老师首页信息
	teacherInfo: function() {
		let that = this;
		app.request({
			url: 'teacher/detail',
			data: {
				teacher_id: that.data.teacherId
			},
			sCallback: (res) => {
				if(res.code == 1) {
					var imgUrls = []
					imgUrls.push(res.data.uploadImg);
					res.data.teacherImages.forEach((item) => {
						imgUrls.push(item.img);
					})

					that.setData({
						alldetail: res.data,
						imgUrls: imgUrls,
					})
				} else {
					app.alert(res.msg,() => {
						wx.navigateBack()
					})
				}
			}
		})
	},
	
	//老師主頁端我的活動
	activity: function() {
		let that = this
		var coursebyTeacher = loginFn()
		coursebyTeacher.teacherId = this.data.teacherId
		wx.request({
			url: urls.coursebyTeacher,
			method: 'GET',
			data: coursebyTeacher,
			success: function(res) {
				// console.log(res, '活動')
				that.setData({
					funList: res.data.data
				})
			}
		})
	},
	// 课程列表
	teacher: function() {
		let that = this;
		app.request({
			url: 'teacher/courseList',
			data: {
				teacher_id: that.data.teacherId
			},
			sCallback:(res) => {
				if(res.code == 1) {
					that.setData({
						teacherlist: res.data
					})
				}
			}
		})
	},
	//時間選擇
	bindTimeChange: function(e) {
		this.setData({
			time: e.detail.value
		})
	},
	//日期選擇
	bindDateChange: function(e) {
		this.setData({
			date: e.detail.value
		})
	},
	checkLogin(){
		var login = setInterval(() => {
			if(wx.getStorageSync('login')){
				clearInterval(login);
				this.init();
			}
		}, 100);
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options)
		that = this;

		if(options.scene) {
			options.teacherId = decodeURIComponent(options.scene).split('&')[0].split('=')[1]
		}

		//获取当前日期给预约日期选择用
		var date = new Date();
        var strDate = date.getFullYear()+'-'+(date.getMonth() + 1)+'-'+date.getDate();
		that.setData({
			date: strDate,
			teacherId: options.teacherId
		})

		

	},
	init() {
		let teacherId = this.data.teacherId;
		//记录访客
		app.visitorEnter({
			title: '老师主页',
			schoolId: '',
			courseId: '',
			teacherId: teacherId,
			from: 3, //统计来源为老师
		}, (visitorId) => {
			that.setData({
				visitorId: visitorId
			});
		});
		// 我的活動
		this.teacher();
		//获取统计数据
		this.teacherTotal()
		//获取老师信息
		this.teacherInfo()
		//评价
		this.getconmentFn()
		
		//用户信息
		var userInfo = wx.getStorageSync('userinfo')
		if (userInfo) {
			this.setData({
				showauth: false,
				userInfo: userInfo
			})
		};

		this.getmyInfoFn() //获取个人信息
		if (this.data.grate.length > 0) {
			//获取学生
			this.getstudentlist()
		} else {
			//获取年级
			this.getgradeFn()
		};
	},
	//所有课程
	getClass: function(loginInfo) {
		that = this
		wx.request({
			url: urls.courseList,
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					// console.log(res, '短短')
					that.setData({
						allClass: res.data.data
					})
				};
			}
		})
	},
	
	//navtab
	scoll_tab: function(e) {
		this.setData({
			status: e.currentTarget.dataset.idx
		})

	},
	//评论列表
	getconmentFn: function(schoolId) {
		app.request({
			url: 'remark/listTeacher',
			data: {
				teacher_id: that.data.teacherId
			},
			sCallback:(res) => {
				if(res.code == 1) {
					that.setData({
						comments: res.data
					})
				}
			}
		})
	},
	//关注
	likeFn: function() {
		var that = this;
		var loginInfo = loginFn();
		loginInfo.schoolId = this.data.alldetail.schoolId;
		wx.request({
			url: urls.focusSchool,
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					var likes = !that.data.haslike
					that.setData({
						haslike: likes
					})
				};
			}
		})
	},
	//想学
	wantFn: function() {
		var that = this;
		var loginInfo = loginFn()
		loginInfo.schoolId = this.data.alldetail.schoolId
		wx.request({
			url: urls.schoolwant,
			data: loginInfo,
			method: 'POST',
			success: function(res) {
				if (res.data.code == '0') {
					wx.showToast({
						title: '想学',
					})
				};
			}
		})
	},
	//评论点赞
	likeremarkFn: function(e) {

		var that = this;
		var userInfo = wx.getStorageSync('userinfo');
		if (userInfo) {
			var loginInfo = loginFn()
			loginInfo.remarkId = e.currentTarget.dataset.id
			wx.request({
				url: urls.likeSchool,
				method: 'POST',
				data: loginInfo,
				success: function(res) {
					if (res.data.code == '0') {
						that.getconmentFn()
						wx.showToast({
							title: '点赞成功',
						})
					};
				}
			})
		} else {
			this.setData({
				showauth: true
			})
		}
	},


	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
		console.log('onReady')
		this.setData({
			current: 0
		})
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		console.log('onShow')
		this.checkLogin();
		

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {
		console.log('onHide')
		app.visitorLeave(this.data.visitorId, () => {
			console.log('发送访客离开时间成功');
		});
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		console.log('onUnload')
		app.visitorLeave(this.data.visitorId, () => {
			console.log('发送访客离开时间成功');
		});
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		return {
			title: '欢迎进入我的个人主页'
		}

	},
	previewFn: function(e) { //预览图片
		wx.previewImage({
			current: e.currentTarget.dataset.url, // 当前显示图片的http链接
			urls: this.data.imgUrls // 需要预览的图片http链接列表
		})
	},
	//去机构管理端
	agentFns: function(res) {
		let that = this
		let statusNum = that.data.statusNum
		wx.navigateTo({
			url: '/pages/institucharge/institucharge',
		})
		//}   
	},
	//去老师管理端
	agentFn: function(e) {
		wx.navigateTo({
			url: '/pages/teacher-info/index?isAdmin=1',
		})
	},

	getmyInfoFn: function() { //获取个人中心
		var that = this;
		app.request({
			url: 'user/index',
			sCallback: (res) => {
				if(res.code == 1) {
					that.setData({
						myInfo: res.data
					})
				}
			}
		})
	},
	addStudentFn: function(e) { //添加学生
		wx.navigateTo({
			url: '/pages/addstudent/index',
		})
	},
	addclassFn: function(e) { //
		wx.navigateTo({
			url: '/pages/addclass/index',
		})
	},
	agentFnn: function(e) {
		wx.navigateTo({
			url: '/pages/addinstitution/index',
		})
	},
	getpromotion: function() { //获取优惠券
		wx.navigateTo({
			url: '/pages/coupons/index?getpromotion=true',
		})
	},
	mycardFn: function() { //我的卡券
		wx.navigateTo({
			url: '/pages/coupons/index?mine=true',
		})
	},
	myorderFn: function(e) { //我的订单
		console.log(e)
		wx.navigateTo({
			url: '/pages/myorder/index?status=' + e.currentTarget.dataset.status,
		})
	},
	loginSuccess: function(e) {
		console.log('安排')
		console.log(e)
	},

	testFn: function(e) { //点击开始测试
		var year = 1000 * 60 * 60 * 24 * 365;
		var now = new Date();
		var birth = e.currentTarget.dataset.birthday;
		birth = Date.parse(birth.replace('/-/g', "/"));
		var birthday = new Date(birth);
		var age = parseInt((now - birthday) / year);
		var questionTypeId = 1
		if (age > 6) {
			questionTypeId = 2
		};
		wx.navigateTo({
			url: '/pages/test/index?studentId=' + e.currentTarget.dataset.id + '&questionTypeId=' + questionTypeId,
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	},
	 //获取学生列表
	getstudentlist: function() {
		var that = this;
		app.request({
			url: 'student/list',
			sCallback:(res) => {
				if(res.code == 1) {
					that.setData({
						studentList: res.data
					})
				}
			}
		})
	},
	//获取年级
	getgradeFn: function() { 
		var that = this;
		app.request({
			url: 'student/grade',
			sCallback:(res) => {
				that.setData({
					grate: res.data
				})
				that.getstudentlist()
			}
		})
	},
	editstudent: function(e) { //编辑学生
		wx.navigateTo({
			url: '/pages/editstudent/index?studentId=' + e.currentTarget.dataset.id + '&idx=' + e.currentTarget.dataset.idx,
		})
	},
	mygoldFn: function(e) { //我的金币
		wx.navigateTo({
			url: '/pages/mygold/index?gold=' + e.currentTarget.dataset.gold
		})
	},
	myscroeFn: function(e) {
		wx.navigateTo({
			url: '/pages/myscore/index?score=' + e.currentTarget.dataset.score
		})
	},
	changeRemark(e){
		this.setData({
			learnpeoIndex: e.currentTarget.dataset.index
		});
	},
	chat: function(){
		wx.navigateTo({
			url: '/pages/message/list/index'
		})
	},
	//隐藏预约
	hide(e) {
		console.log(e);
		this.setData({
			showView: false
		})
	}
})
